<?php
class Order_model extends CI_Model
{
	function __construct()
	{
		parent::__construct();
	}
	function getOrderNum($userInfo=null)
	{
		$this->db->select();
		$this->db->where('order.for_user',$userInfo['id']);
		$this->db->where('order.shop_type !=','1');
		$data['total_rows'] = $this->db->get('order')->num_rows();

		$this->db->select();
		$this->db->where('order.for_user',$userInfo['id']);
		$this->db->where('order.shop_type !=','1');
		$this->db->where_in('order.static',0);
		$data['nopay_rows'] = $this->db->get('order')->num_rows();

		$this->db->select();
		$this->db->where('order.for_user',$userInfo['id']);
		$this->db->where('order.shop_type !=','1');
		$this->db->where_in('order.static',1);
		$data['nook_rows'] = $this->db->get('order')->num_rows();

		$this->db->select();
		$this->db->where('order.for_user',$userInfo['id']);
		$this->db->where('order.shop_type !=','1');
		$this->db->where_in('order.static',2);
		$data['ok_rows'] = $this->db->get('order')->num_rows();

		$this->db->select();
		$this->db->where('order.for_user',$userInfo['id']);
		$this->db->where('order.shop_type !=','1');
		$this->db->where_in('order.static',3);
		$data['close_rows'] = $this->db->get('order')->num_rows();
		return $data;
	}
	function getOrder($static,$p,$n,$userInfo=null)
	{
		$this->db->select();
		if($userInfo)
		{
			$this->db->where('order.for_user',$userInfo['id']);
		}
		if($static != 'all')
		{
			$this->db->where('order.static',$static);
		}
		else
		{
			$this->db->where('order.static !=',2);
		}
		$this->db->where('order.shop_type !=','1');
		$this->db->order_by('order.static', 'asc');
		$this->db->order_by('order.crea_time','DESC');
		$this->db->limit($n,($p-1)*$n);
		$order = $this->db->get('order')->result_array();
		foreach ($order as $k=>$v)
		{
			$this->db->select();
			$this->db->where('order_id =',$v['order_id']);
			$order[$k]['info'] = $this->db->get('addorder')->result_array();
		}
		return $order;
	}
	function addOrder($data,$userInfo=null,$type='one')
	{
		$shop_type = ($type == 'more')?0:$data['shop_type'];
		$order['for_user'] = $userInfo['id'];
		$order['shop_type'] = $shop_type;
		$order['crea_time'] = time();
		$this->db->insert('order', $order);
		$new_id = $this->db->insert_id('order');
		$order_id = date('mdYHis',time()).$new_id;
		$this->db->where('id',$new_id);
		$this->db->update('order',array('order_id'=>$order_id));

		if($type == 'more')
		{
			foreach ($data as $k=>$v)
			{
				$orderInfo = $v;
				$orderInfo['order_id'] = $order_id;
				$this->db->insert('addorder', $orderInfo);
			}
		}
		else
		{
			$orderInfo = $data;
			unset($orderInfo['shop_type']);
			$orderInfo['order_id'] = $order_id;
			$this->db->insert('addorder', $orderInfo);
		}
		return $order_id;
	}
	function getOrderById($order_id)
	{
		$this->db->select();
		$this->db->where('order_id',$order_id);
		$order = $this->db->get('order')->result_array();

		$this->db->where('order_id',$order_id);
		$order[0]['info'] = $this->db->get('addorder')->result_array();
		return $order;
	}
	function getOrderForRid($rid)
	{
		$this->db->select();
		$this->db->where('id',$rid);
		$orderInfo = $this->db->get('addorder')->result_array();
		return $orderInfo[0];
	}
	function getOrderByNumber($number)
	{
		$this->db->select();
		$this->db->where('addorder.post_number',$number);
		$orderInfo = $this->db->get('addorder')->result_array();
		return $orderInfo;
	}
	function getOrderField($order_id,$field)
	{
		$this->db->select($field);
		$this->db->where('order_id',$order_id);
		$data = $this->db->get('order')->result_array();
		return $data[0][$field];
	}
	function setOrder($id,$data)
	{
		$this->db->where('id',$id);
		$this->db->update('addorder',$data);
	}
	function sendOrder($id,$data)
	{
		$this->db->where('id',$id);
		$this->db->update('order',$data);
	}
	function getOrderInfoNum($order_id)
	{
		$this->db->select();
		$this->db->where('order_id',$order_id);
		return $this->db->get('addorder')->num_rows();
	}
}